#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = 'arrow'
__explain__ = ''

import datetime
import os
import posixpath


def custom_file_path(flag, filename, extra_path="") -> str:
    upload_to = f"{flag}/%Y/%m/%d/{extra_path}"
    dirname = datetime.datetime.now().strftime(str(upload_to))
    filename = posixpath.join(dirname, filename)
    return filename


def plat_file_upload_to(instance, filename) -> str:
    """
    对视频文件进行分类保存
    :param instance: MaterialFile对象
    :param filename: 文件名
    :return: path
    """
    return custom_file_path(instance.source.lower(), filename)


def thumbnail_upload_to(instance, filename) -> str:
    """
    对缩略图保存
    :param instance: VideoCarrier对象
    :param filename: 文件名
    :return: path
    """
    return custom_file_path(instance.source.lower(), filename, extra_path="thumbnail/")


def avatar_upload_to(instance, filename) -> str:
    """
    对avatar保存
    :param instance: ChannelSubscription对象
    :param filename: 文件名
    :return: path
    """
    return custom_file_path(instance.plat_from.code.lower(), filename, extra_path="avatar/")


def create_dir(full_path):
    directory = os.path.dirname(full_path)
    try:
        os.makedirs(directory, exist_ok=True)
    except FileExistsError:
        raise FileExistsError('%s exists and is not a directory.' % directory)